package com.frame.work.frame.deal.device0fc8;


import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.frame.deal.base.BaseFrame;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 单相电表
 */
@Data
public class DanXiangDianBiao_0FC80102 extends BaseFrame {

    private String switchState;

    public DanXiangDianBiao_0FC80102(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据
        this.setSwitchState(StringUtils.substring(data, data.length() - 2, data.length()));

        //电压
        String dianya = StringUtils.substring(data, data.length() - 26, data.length() - 22);
        dianya = StringUtils.substring(dianya, 2, 4) + StringUtils.substring(dianya, 0, 2);
        dianya = StringUtils.substring(dianya, 0, 3) + '.' + StringUtils.substring(dianya, 3, 4);
        //电流
        String dianliu = StringUtils.substring(data, data.length() - 22, data.length() - 16);
        dianliu = StringUtils.substring(dianliu, 4, 6) + StringUtils.substring(dianliu, 2, 4) + StringUtils.substring(dianliu, 0, 2);
        dianliu = StringUtils.substring(dianliu, 0, 3) + '.' + StringUtils.substring(dianliu, 3, 6);
        //功率
        String gonglv = StringUtils.substring(data, data.length() - 16, data.length() - 10);
        gonglv = StringUtils.substring(gonglv, 4, 6) + StringUtils.substring(gonglv, 2, 4) + StringUtils.substring(gonglv, 0, 2);
        gonglv = StringUtils.substring(gonglv, 0, 5) + '.' + StringUtils.substring(gonglv, 5, 6);
        //电量
        String dianliang = StringUtils.substring(data, data.length() - 10, data.length() - 2);
        dianliang = StringUtils.substring(dianliang, 6, 8) + StringUtils.substring(dianliang, 4, 6) + StringUtils.substring(dianliang, 2, 4) + StringUtils.substring(dianliang, 0, 2);
        dianliang = StringUtils.substring(dianliang, 0, 6) + '.' + StringUtils.substring(dianliang, 6, 8);


        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("dianya", dianya);
        actions.put("dianliu", dianliu);
        actions.put("gonglv", gonglv);
        actions.put("dianliang", dianliang);
        actions.put("switchState", switchState);
        this.setActions(actions);

        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);
    }

}
